// ListViewPeer.cpp : implementation file
//

#include "stdafx.h"
#include "ListViewPeer.h"
#include "ListView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// ListViewPeer

ListViewPeer::ListViewPeer(AWTComponent* comp)
{
	m_comp = comp;
}

ListViewPeer::~ListViewPeer()
{
}


BEGIN_MESSAGE_MAP(ListViewPeer, CListCtrl)
	//{{AFX_MSG_MAP(ListViewPeer)
	ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
	ON_NOTIFY_REFLECT(HDN_ITEMCLICK, OnItemclick)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ListViewPeer message handlers

void ListViewPeer::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	LVITEM lvi = {0};

//	CString s = GetItemText(pNMListView->iItem, 0);

	ListView* lv = (ListView*)m_comp;

	if (pNMListView->uNewState == 3)
	{
		((ListView*)m_comp)->setSel(pNMListView->iItem);
		AWTItemEvent ev(lv, pNMListView->iItem, GetItemData(pNMListView->iItem), AWTItemEvent::SELECTED);
		lv->processItemEvent(&ev);
	}
	else if (pNMListView->uNewState == 0)
	{
		AWTItemEvent ev(lv, pNMListView->iItem, GetItemData(pNMListView->iItem), AWTItemEvent::DESELECTED);
		lv->processItemEvent(&ev);
	}

	*pResult = 0;
}

void ListViewPeer::OnItemclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void ListViewPeer::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;

	ListView* lv = (ListView*)m_comp;
	lv->columnClicked(pNMListView->iSubItem);
}
